package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.BannerMapper;
import cn.scxy.bookcity.pojo.dto.BannerAddDTO;
import cn.scxy.bookcity.pojo.entity.Banner;
import cn.scxy.bookcity.service.IBannerService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
@Slf4j
public class BannerServiceImpl implements IBannerService {
    @Value("d:/files")
    private String dirPath;

    @Autowired
    BannerMapper bannerMapper;


    @Override
    public void insert(BannerAddDTO bannerAddDTO) {
        int insert = bannerMapper.insert("http://localhost:8080/"+bannerAddDTO.getUrl());
        if (insert != 1) {
            String message = "添加轮播图失败，服务器忙，请稍后再尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(int id) {
       /* String url = bannerMapper.selectUrlById(id);
        new File(dirPath+url).delete();*/
        bannerMapper.deleteById(id);
    }

    @Override
    public List<Banner> list() {
        log.debug("开始处理[查询轮播图列表]的业务,无参数");
        return bannerMapper.list();
    }


    @Override
    public void updateById(Banner banner) {
        int update = bannerMapper.update(banner);
        if(update!=1){
            String message="服务器忙";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }
}
